#include <gnome.h>
#include "list_model.h"
#include "interface.h"
#include "monitor.h"

GtkTreeModel *
create_model_Monitor (void)
{
	GtkListStore  *listStore;
	GtkTreeIter    iter;

	listStore = gtk_list_store_new (
		MONITOR_NUM_COLS,
		G_TYPE_BOOLEAN, 
		G_TYPE_STRING,
		G_TYPE_POINTER);
	return GTK_TREE_MODEL(listStore);
}

void
fill_model_Monitor(GtkListStore *listStore)
{
	GtkTreeIter  iter;
	int i;
	extern monitor monitors[];

	gtk_list_store_clear(listStore);
	for(i=0;i<MONITOR_TYPE_COUNT;i++) {
		gtk_list_store_append(listStore, &iter);
		gtk_list_store_set(listStore, &iter,
				MONITOR_COL_ACTIVE, monitors[i].active,
				MONITOR_COL_TITLE,  monitors[i].title,
				MONITOR_STRUCT,     &(monitors[i]),
				-1);	
	}
}

GtkWidget *
create_view_and_model_Monitor (GtkWidget *appW3Live)
{
	GtkTreeViewColumn   *col;
	GtkCellRenderer     *renderer;
	GtkCellRenderer     *toggleRender;
	GtkWidget           *view;
	GtkTreeModel        *model;
	GtkTreeSelection    *sel;
	GtkTreePath         *path;
	
	void **args;

	model = create_model_Monitor();
	fill_model_Monitor(model);

	view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));

	toggleRender = gtk_cell_renderer_toggle_new();
	sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));

	gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);

	/* --- Column #1 --- */
	col = gtk_tree_view_column_new_with_attributes (
		_("Active"),
   	toggleRender,
		"active", 0,
		NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
	//gtk_tree_view_column_pack_start(col, toggleRender, TRUE);

	/* --- Column #2 --- */
	renderer = gtk_cell_renderer_text_new();
	col = gtk_tree_view_column_new_with_attributes(
			_("Title"), renderer,"text",1,NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
	
	g_object_unref(G_OBJECT(model));
	
	if(!(args = (void **)malloc(sizeof(void *)*2)))
		perror_exit("malloc (%s:%d)",__FILE__,__LINE__);

	args[0] = model;
	args[1] = appW3Live;
	
	g_signal_connect(G_OBJECT(sel), "changed",
			G_CALLBACK (monitor_sel),
			args);
	
	g_signal_connect(G_OBJECT(toggleRender), "toggled",
			G_CALLBACK (monitor_load),
			args);

	path = gtk_tree_path_new_first();
	gtk_tree_selection_select_path(sel,path);
	gtk_tree_path_free(path);
		
	return view;
}

void 
monitor_sel (GtkTreeSelection *sel, gpointer user_data)
{
	GtkTreeModel *model;
	GtkWidget *appW3Live;
	GtkTreeIter iter;
	GValue val = { 0, };
	monitor *m;

	GtkTextView *text = NULL;
	GtkTextBuffer *buffer = NULL;
	GtkWidget *radio = NULL;
	GtkWidget *box = NULL, *table;
	
	model     = (GtkTreeModel *)((void **)user_data)[0];
	appW3Live = (GtkWidget *)((void **)user_data)[1];

	if(!gtk_tree_selection_get_selected(sel,&model,&iter))
		return;

	gtk_tree_model_get_value(model,&iter, MONITOR_STRUCT, &val);
	m = g_value_get_pointer(&val);
	
	text = (GtkTextView *)lookup_widget(appW3Live,"textviewMonitorDescription");
	
	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
	gtk_text_buffer_set_text(buffer,m->about,-1);
	
	/* Display proper configuration thinger */
	box = (GtkWidget *)lookup_widget(appW3Live,"vboxConfigure");
	if((table = lookup_widget(appW3Live,"conftable")))
		gtk_widget_destroy(table);
	if((table = ((m->conf).form)(appW3Live,m)))
		gtk_box_pack_start(GTK_BOX(box),table,TRUE,TRUE,0);
	
	/* Make the action display proper like */
	switch(m->action.type) {
	case MONITOR_ACTION_POPUP:
		radio = (GtkWidget *)lookup_widget(appW3Live,"radiobuttonActionPopup");
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio), TRUE);
		break;
	case MONITOR_ACTION_SCRIPT:
		radio = (GtkWidget *)lookup_widget(appW3Live,"radiobuttonActionExecute");
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio), TRUE);
		/* set the script path ! */
		break;
	case MONITOR_ACTION_EMAIL:
		break;
	}

}

void
monitor_unload(GtkTreeModel *model, w3lclient *w3lc, GtkTreePath *path, monitor *m)
{
	GtkTreeIter iter;

	gtk_tree_model_get_iter (model, &iter, path);
	gtk_tree_model_get(model, &iter, MONITOR_STRUCT, &m, -1);

	w3lRequest(w3lc,"unwatch(%d)",m->id);
	m->active = 0;
	gtk_list_store_set(GTK_LIST_STORE (model), &iter, MONITOR_COL_ACTIVE, FALSE, -1);
}

void
monitor_load(GtkCellRendererToggle *cell, gchar *pth, gpointer user_data)
{
	GtkTreeModel *model;
	GtkWidget *appW3Live;
	GtkTreeIter iter;
	GtkTreePath *path = gtk_tree_path_new_from_string(pth);
	monitor *m;
	w3lclient *w3lc;

   model     = (GtkTreeModel *)((void **)user_data)[0];
   appW3Live = (GtkWidget *)((void **)user_data)[1];

	w3lc = (w3lclient *)lookup_widget(appW3Live,"w3lc");

	gtk_tree_model_get_iter (model, &iter, path);
	gtk_tree_model_get(model, &iter, MONITOR_STRUCT, &m, -1);
	
	if(m->active) {
		monitor_unload(model,w3lc,path,m);
		return;
	}
	
	/* establish watch with daemon */
	if((m->w3ld_init)(w3lc,m))
		gtk_list_store_set(GTK_LIST_STORE (model), &iter, MONITOR_COL_ACTIVE, TRUE, -1);
	else
		gtk_list_store_set(GTK_LIST_STORE (model), &iter, MONITOR_COL_ACTIVE, FALSE, -1);
	
}

